package com.ycy.smartcourseselection.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycy.smartcourseselection.common.PageParamsDTO;
import com.ycy.smartcourseselection.common.PageResVO;
import com.ycy.smartcourseselection.common.ResCodeEnum;
import com.ycy.smartcourseselection.common.ResData;
import com.ycy.smartcourseselection.dto.courseSelection.FileCoSelectionDTO;
import com.ycy.smartcourseselection.dto.score.*;
import com.ycy.smartcourseselection.dto.student.FindAllStuDTO;
import com.ycy.smartcourseselection.entity.*;
import com.ycy.smartcourseselection.exception.CustomException;
import com.ycy.smartcourseselection.mapper.IScoreMapper;
import com.ycy.smartcourseselection.utils.PageUtils;
import com.ycy.smartcourseselection.utils.WrapperUtils;
import com.ycy.smartcourseselection.vo.course.CourseVO;
import com.ycy.smartcourseselection.vo.courseSelection.FileCoSelectionVO;
import com.ycy.smartcourseselection.vo.score.FileScoreVO;
import com.ycy.smartcourseselection.vo.score.ScoreAnalyseVO;
import com.ycy.smartcourseselection.vo.score.ScoreStuChartVO;
import com.ycy.smartcourseselection.vo.score.ScoreVO;
import com.ycy.smartcourseselection.vo.student.FileStuVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.*;

@Service
public class ScoreService extends ServiceImpl<IScoreMapper, Score> {
    PageUtils<Score, ScoreVO> scorePageUtils = new PageUtils<>();

    @Resource
    TeacherService teacherService;
    @Resource
    StudentService studentService;
    @Resource
    CourseService courseService;
    @Resource
    CoSelectionService coSelectionService;

    //批量增加或修改成绩
    public ResData<Boolean> saveScore(ScoreDTO scoreDTO) {
        ArrayList<Score> addList = scoreDTO.getAddList();
        ArrayList<Score> updateList = scoreDTO.getUpdateList();

        for(Score entity: addList) {
            if(entity.getId() != null) throw new CustomException(ResCodeEnum.BAD_REQUEST,"添加的分数对象数组中有包含id的项");
            validateScore(entity);
        }

        for(Score entity: updateList) {
            if(entity.getId() == null) throw new CustomException(ResCodeEnum.BAD_REQUEST,"修改的分数对象数组中有未包含id项");
            validateScore(entity);
        }

        boolean saveFlag = true;
        boolean updateFlag = true;
        if(addList.size() != 0) {
            saveFlag = saveBatch(addList);
        }
        if(updateList.size() != 0) {
            updateFlag = updateBatchById(updateList);
        }

        return ResData.success(saveFlag && updateFlag);
    }

    //分页查成绩
    public ResData<PageResVO<ScoreVO>> findPageScore(FindPageScoreDTO findPageScoreDTO) {
        PageParamsDTO pageParamsDTO = findPageScoreDTO.getPageParams();
        FindAllScoreDTO otherParams = findPageScoreDTO.getOtherParams();

        //验证pageNum，pageSize
        Integer pageNum = pageParamsDTO.getPageNum();
        Integer pageSize = pageParamsDTO.getPageSize();
        scorePageUtils.validatePageNumPageSize(pageNum, pageSize);

        String workId = otherParams.getWorkId();
        String studentId = otherParams.getStudentId();
        Integer grade = otherParams.getGrade();
        Integer stage = otherParams.getStage();
        String college = otherParams.getCollege();
        String schoolYear = otherParams.getSchoolYear();
        String major = otherParams.getMajor();
        String stuClass = otherParams.getStuClass();
        String stuName = otherParams.getStuName();
        String courseId = otherParams.getCourseId();
        String tchName = otherParams.getTchName();
        String teachClass = otherParams.getTeachClass();
        String courseCode = otherParams.getCourseCode();
        Double scoreRangeMin = null;
        Double scoreRangeMax = null;
        if(otherParams.getScoreRange() != null && otherParams.getScoreRange().size() == 2) {
            scoreRangeMin = otherParams.getScoreRange().get(0);
            scoreRangeMax = otherParams.getScoreRange().get(1);
        }

        ArrayList<String> tchConditionList = new ArrayList<>();
        if(StrUtil.isNotBlank(workId) || StrUtil.isNotBlank(tchName)) {
            ArrayList<String> workIdList = new ArrayList<>();
            QueryWrapper<Teacher> tchWrapper = new QueryWrapper<>();
            tchWrapper.eq(StrUtil.isNotBlank(workId),"work_id",workId);
            tchWrapper.like(StrUtil.isNotBlank(tchName),"name",tchName);
            tchWrapper.select("work_id");
            List<Teacher> tchList = teacherService.list(tchWrapper);
            if(tchList.size() == 0) return returnEmptyRes();
            for (Teacher teacher: tchList) workIdList.add(teacher.getWorkId());

            QueryWrapper<Course> courseWrapper = new QueryWrapper<>();
            courseWrapper.in("work_id",workIdList);
            courseWrapper.select("id");
            List<Course> courseList = courseService.list(courseWrapper);
            if(courseList.size() == 0) return returnEmptyRes();
            for (Course course: courseList) tchConditionList.add(course.getId());
        }

        ArrayList<String> courseConditionList = new ArrayList<>();
        if(StrUtil.isNotBlank(teachClass) || StrUtil.isNotBlank(courseCode) || StrUtil.isNotBlank(schoolYear)) {
            QueryWrapper<Course> courseWrapper = new QueryWrapper<>();
            courseWrapper.like(StrUtil.isNotBlank(teachClass),"teach_class",teachClass);
            courseWrapper.eq(StrUtil.isNotBlank(courseCode),"course_code",courseCode);
            courseWrapper.eq(StrUtil.isNotBlank(schoolYear),"school_year",schoolYear);
            courseWrapper.eq(grade != null,"grade",grade);
            courseWrapper.eq(stage != null,"stage",stage);
            courseWrapper.select("id");
            List<Course> courseList = courseService.list(courseWrapper);
            if(courseList.size() == 0) return returnEmptyRes();
            for (Course course: courseList) courseConditionList.add(course.getId());
        }

        System.out.println("tchConditionList"+tchConditionList);
        System.out.println("courseConditionList"+courseConditionList);
        QueryWrapper<CoSelection> coSelectionWrapper = new QueryWrapper<>();
        if(tchConditionList.size() != 0 && courseConditionList.size() == 0) coSelectionWrapper.in("course_id",tchConditionList);
        if(courseConditionList.size() != 0 && tchConditionList.size() == 0) coSelectionWrapper.in("course_id",courseConditionList);
        if(tchConditionList.size() != 0 && courseConditionList.size() != 0) {
            ArrayList<String> sameCourseIdList = new ArrayList<>();
            for (String tchConditionCourseId: tchConditionList) {
                for (String courseConditionCourseId: courseConditionList) {
                    if(tchConditionCourseId.equals(courseConditionCourseId)) sameCourseIdList.add(courseConditionCourseId);
                }
            }
            // HashSet<String> sameCourseIdList = new HashSet<>(tchConditionList);
            // sameCourseIdList.addAll(courseConditionList);
            coSelectionWrapper.in("course_id",sameCourseIdList);
        }
        coSelectionWrapper.eq(StrUtil.isNotBlank(courseId),"course_id",courseId);
        coSelectionWrapper.eq(StrUtil.isNotBlank(studentId),"student_id",studentId);
        coSelectionWrapper.select("student_id","course_id");
        List<CoSelection> coSelectionList = coSelectionService.list(coSelectionWrapper);
        if(coSelectionList.size() == 0) return returnEmptyRes();

        ArrayList<String> stuIdList = new ArrayList<>();
        HashSet<String> courseIdList = new HashSet<>();
        for (CoSelection entity :coSelectionList) {
            stuIdList.add(entity.getStudentId());
            courseIdList.add(entity.getCourseId());
        }

        QueryWrapper<Course> courseWrapper = new QueryWrapper<>();
        courseWrapper.in("id",courseIdList);
        courseWrapper.select("course_name","id","course_code","work_id","teach_class","school_year","stage");
        List<Course> courseList = courseService.list(courseWrapper);
        if(courseList.size() == 0) return returnEmptyRes();

        QueryWrapper<Student> stuWrapper = new QueryWrapper<>();
        if((StrUtil.isNotBlank(studentId))) stuWrapper.eq("student_id",studentId);
        else stuWrapper.in("student_id",stuIdList);
        stuWrapper.like(StrUtil.isNotBlank(stuClass),"stu_class",stuClass);
        stuWrapper.like(StrUtil.isNotBlank(stuName),"name",stuName);
        stuWrapper.like(StrUtil.isNotBlank(major),"major",major);
        stuWrapper.like(StrUtil.isNotBlank(college),"college",college);
        stuWrapper.eq(grade != null,"grade",grade);
        stuWrapper.select("name","college","major","stu_class","grade","student_id");
        List<Student> stuList = studentService.list(stuWrapper);
        if(stuList.size() == 0) return returnEmptyRes();

        QueryWrapper<Score> scoreWrapper = new QueryWrapper<>();
        scoreWrapper.between(scoreRangeMin != null && scoreRangeMax != null,"score",scoreRangeMin,scoreRangeMax);
        scoreWrapper.eq(StrUtil.isNotBlank(studentId),"student_id",studentId);
        QueryWrapper<Score> newQueryWrapper = scorePageUtils.buildPageQueryWrapper(pageParamsDTO, scoreWrapper);

        // Page<Score> basePage = new Page<>(pageNum, pageSize);
        // Page<Score> resPage = page(basePage, newQueryWrapper);
        List<Score> scoreList = list(newQueryWrapper);
        // if(resPage.getRecords().size() == 0) return returnEmptyRes();

        List<Teacher> tchList = null;
        if(scoreList.size() != 0) {
            HashSet<String> workIdList = new HashSet<>();
            for (Score entity : scoreList) {
                workIdList.add(entity.getWorkId());
            }

            QueryWrapper<Teacher> tchWrapper = new QueryWrapper<>();
            tchWrapper.in("work_id",workIdList);
            tchWrapper.select("name","work_id");
            tchList = teacherService.list(tchWrapper);
        }

        ArrayList<ScoreVO> VOList = new ArrayList<>();
        for (CoSelection coSelection :coSelectionList) {
            ScoreVO scoreVO = new ScoreVO();
            for (Student stuEntity: stuList) {
                if(stuEntity.getStudentId().equals(coSelection.getStudentId())) {
                    scoreVO.setStuName(stuEntity.getName());
                    scoreVO.setCollege(stuEntity.getCollege());
                    scoreVO.setMajor(stuEntity.getMajor());
                    scoreVO.setStuClass(stuEntity.getStuClass());
                    scoreVO.setGrade(stuEntity.getGrade());
                    scoreVO.setStudentId(stuEntity.getStudentId());
                }
            }
            scoreVO.setCourseId(coSelection.getCourseId());
            for (Course course : courseList) {
                if (scoreVO.getCourseId().equals(course.getId())) {
                    scoreVO.setCourseName(course.getCourseName());
                    scoreVO.setCourseCode(course.getCourseCode());
                    scoreVO.setTeachClass(course.getTeachClass());
                    scoreVO.setSchoolYear(course.getSchoolYear() + "学年");
                    scoreVO.setStage(courseService.toChineseStage(course.getStage().toString()));
                    // scoreVO.setCourseId(course.getId());
                }
            }
            if(scoreVO.getStudentId() != null) VOList.add(scoreVO);
        }

        for (Score entity : scoreList) {
            for (ScoreVO scoreVO : VOList) {
                if (scoreVO.getStudentId().equals(entity.getStudentId()) && scoreVO.getCourseId().equals(entity.getCourseId()))
                    BeanUtil.copyProperties(entity, scoreVO, "studentId", "courseId");  //复制了id,workId,createTime，score

                if (tchList != null) {
                    for (Teacher teacher : tchList) {
                        if (teacher.getWorkId().equals(scoreVO.getWorkId()))
                            scoreVO.setTchName(teacher.getName());
                    }
                }
            }
        }

        if(scoreRangeMin != null && scoreRangeMax != null) {
            if(scoreRangeMin != 0 && scoreRangeMax != 60)
                for (int i = 0; i < VOList.size(); i++) {
                    ScoreVO scoreVO = VOList.get(i);
                    if(StrUtil.isBlank(scoreVO.getId())) {
                        VOList.remove(scoreVO);
                        i--;
                    }
                }
            else if(scoreRangeMin == 0 && scoreRangeMax == 60) {
                QueryWrapper<Score> wrapper = new QueryWrapper<>();
                wrapper.eq(StrUtil.isNotBlank(studentId),"student_id",studentId);
                List<Score> scoreNewList = list(wrapper);

                for (int i = 0; i < VOList.size(); i++) {
                    ScoreVO scoreVO = VOList.get(i);
                    for (Score score: scoreNewList) {
                        if(score.getCourseId().equals(scoreVO.getCourseId()) && StrUtil.isBlank(scoreVO.getId())) {
                            VOList.remove(scoreVO);
                            i--;
                        }
                    }
                }
            }
        }

        //构造分页结果
        PageResVO<ScoreVO> pageResVO = scorePageUtils.getCustomPageResVO(pageNum,pageSize,VOList);

        return ResData.success(pageResVO);
    }

    //按课程分析图表数据(教师角色)
    public ResData<ScoreAnalyseVO> findScoreAnalyseData(String courseId) {
        if(StrUtil.isBlank(courseId)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"课程id不能为空");
        ScoreAnalyseVO scoreAnalyseVO = new ScoreAnalyseVO();

        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.orderByDesc("score");
        List<Score> scoreList = list(wrapper);
        int listSize = scoreList.size();
        if(listSize == 0) return ResData.success(scoreAnalyseVO);

        String[] lineXAxis = { "0-59.99","60-69.99","70-79.99","80-89.99","90-100" };
        Integer[] lineYAxis = { 0,0,0,0,0 };
        double sumScore = 0;
        for(Score score: scoreList) sumScore += score.getScore();  //得到平均分
        double aveScore = sumScore / listSize;

        int overAveNum = 0;
        LinkedList<String> studentIdList = new LinkedList<>();
        for(Score score: scoreList) {
            Double scoreNum = score.getScore();
            studentIdList.add(score.getStudentId());
            if(scoreNum >= aveScore) overAveNum++;
            if(scoreNum >= 0 && scoreNum <= 59.99) lineYAxis[0]++;
            if(scoreNum >= 60 && scoreNum <= 69.99) lineYAxis[1]++;
            if(scoreNum >= 70 && scoreNum <= 79.99) lineYAxis[2]++;
            if(scoreNum >= 80 && scoreNum <= 89.99) lineYAxis[3]++;
            if(scoreNum >= 90 && scoreNum <= 100) lineYAxis[4]++;
        }

        QueryWrapper<Student> stuWrapper = new QueryWrapper<>();
        stuWrapper.select("name","student_id");
        stuWrapper.in(studentIdList.size() != 0,"student_id",studentIdList);
        List<Student> stuList = studentService.list(stuWrapper);

        ArrayList<HashMap<String, String>> personalRankList = new ArrayList<>();
        for(int i = 1; i <= scoreList.size(); i++) {
            Score score = scoreList.get(i-1);
            HashMap<String, String> personalRank = new HashMap<>();  //stuName、studentId、rank、score
            for(Student student: stuList) {
                if(score.getStudentId().equals(student.getStudentId())) {
                    personalRank.put("stuName",student.getName());
                    personalRank.put("studentId",student.getStudentId());
                    personalRank.put("rank",Integer.toString(i));
                    personalRank.put("score",Double.toString(score.getScore()));
                    personalRankList.add(personalRank);
                    break;
                }
            }
        }


        scoreAnalyseVO.setLineXAxis(lineXAxis);
        scoreAnalyseVO.setLineYAxis(lineYAxis);
        scoreAnalyseVO.setMaxScore(scoreList.get(0).getScore());
        scoreAnalyseVO.setMinScore(scoreList.get(listSize -1).getScore());
        scoreAnalyseVO.setAveScore(aveScore);
        scoreAnalyseVO.setHasScoreNum(listSize);
        scoreAnalyseVO.setOverAveNum(overAveNum);
        scoreAnalyseVO.setPersonalRankList(personalRankList);

        return ResData.success(scoreAnalyseVO);
    }

    //按学生分析图表数据(学生角色)
    public ResData<ScoreStuChartVO> findScoreStuChartData(ScoreStuChartDTO scoreStuChartDTO) {
        String studentId = scoreStuChartDTO.getStudentId();
        if(StrUtil.isBlank(studentId)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"学号不能为空");
        String schoolYear = scoreStuChartDTO.getSchoolYear();
        Integer stage = scoreStuChartDTO.getStage();

        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id",studentId);
        List<Score> scoreList = list(wrapper);
        if(scoreList.size() == 0) return ResData.success(new ScoreStuChartVO(),"该条件下无成绩信息");

        LinkedList<String> courseIdList = new LinkedList<>();
        for (Score score: scoreList) courseIdList.addFirst(score.getCourseId());

        QueryWrapper<Course> courseWrapper = new QueryWrapper<>();
        courseWrapper.in(courseIdList.size() != 0,"id",courseIdList);
        courseWrapper.eq(StrUtil.isNotBlank(schoolYear),"school_year",schoolYear);
        courseWrapper.eq(stage != null,"stage",stage);
        courseWrapper.orderByDesc("school_year");
        courseWrapper.select("course_name","id");
        List<Course> courseList = courseService.list(courseWrapper);
        if(courseList.size() == 0) return ResData.success(new ScoreStuChartVO(),"该条件下无成绩信息");

        LinkedList<String> lineXList = new LinkedList<>();
        LinkedList<Double> lineYList = new LinkedList<>();
        LinkedList<HashMap<String,String>> perScoreList = new LinkedList<>();
        String[] pieLabelList = { "不及格","及格","良好","优秀" };
        Integer[] pieValueList = { 0,0,0,0 };
        double scoreSum = 0;
        for (Course course : courseList) {
            for (Score score : scoreList) {
                if (score.getCourseId().equals(course.getId())) {
                    HashMap<String, String> perScoreMap = new HashMap<>();
                    Double scoreNum = score.getScore();
                    if (scoreNum >= 0 && scoreNum < 60) {
                        pieValueList[0]++;
                        perScoreMap.put("level", pieLabelList[0]);
                    }
                    if (scoreNum >= 60 && scoreNum < 70) {
                        pieValueList[1]++;
                        perScoreMap.put("level", pieLabelList[1]);
                    }
                    if (scoreNum >= 70 && scoreNum < 90) {
                        pieValueList[2]++;
                        perScoreMap.put("level", pieLabelList[2]);
                    }
                    if (scoreNum >= 90 && scoreNum <= 100) {
                        pieValueList[3]++;
                        perScoreMap.put("level", pieLabelList[3]);
                    }
                    scoreSum += scoreNum;
                    lineYList.add(scoreNum);
                    perScoreList.add(perScoreMap);
                    lineXList.add(course.getCourseName());
                    perScoreMap.put("score", scoreNum.toString());
                    perScoreMap.put("courseName", course.getCourseName());
                }
            }
        }

        ScoreStuChartVO scoreStuChartVO = new ScoreStuChartVO();
        scoreStuChartVO.setPerScoreList(perScoreList);
        scoreStuChartVO.setLineYList(lineYList);
        scoreStuChartVO.setLineXList(lineXList);
        scoreStuChartVO.setPieLabelList(pieLabelList);
        scoreStuChartVO.setPieValueList(pieValueList);
        scoreStuChartVO.setPassRate((pieValueList[1] + pieValueList[2] + pieValueList[3]) / Double.parseDouble(Integer.toString(lineXList.size())));
        scoreStuChartVO.setAveScore(scoreSum / lineXList.size());

        return ResData.success(scoreStuChartVO);
    }

    //导出某一课程的分数表格
    public void scoreExport(HttpServletResponse response, FileScoreDTO fileScoreDTO) {
        String courseId = fileScoreDTO.getCourseId();
        if (StrUtil.isBlank(courseId)) throw new CustomException(ResCodeEnum.BAD_REQUEST, "课程id不能为空");

        QueryWrapper<Course> courseWrapper = new QueryWrapper<>();
        courseWrapper.eq("id",courseId);
        courseWrapper.select("teach_class","course_name","course_code");
        List<Course> courseList = courseService.list(courseWrapper);
        if(courseList.size() == 0) throw new CustomException(ResCodeEnum.BAD_REQUEST,"该课程id的信息不存在");
        if(courseList.size() > 1) throw new CustomException(ResCodeEnum.BAD_REQUEST,"错误地存在多个该课程id的信息");
        Course course = courseList.get(0);

        String studentId = fileScoreDTO.getStudentId();
        String stuName = fileScoreDTO.getStuName();
        String stuClass = fileScoreDTO.getStuClass();
        String major = fileScoreDTO.getMajor();
        String college = fileScoreDTO.getCollege();
        Integer grade = fileScoreDTO.getGrade();
        Double scoreRangeMin = null;
        Double scoreRangeMax = null;
        if(fileScoreDTO.getScoreRange() != null && fileScoreDTO.getScoreRange().size() == 2) {
            scoreRangeMin = fileScoreDTO.getScoreRange().get(0);
            scoreRangeMax = fileScoreDTO.getScoreRange().get(1);
        }

        QueryWrapper<Student> stuWrapper = new QueryWrapper<>();
        stuWrapper.eq(grade != null,"grade",grade);
        stuWrapper.eq(StrUtil.isNotBlank(studentId),"student_id",studentId);
        stuWrapper.like(StrUtil.isNotBlank(college),"college",college);
        stuWrapper.eq(StrUtil.isNotBlank(stuName),"name",stuName);
        stuWrapper.eq(StrUtil.isNotBlank(stuClass),"stu_class",stuClass);
        stuWrapper.like(StrUtil.isNotBlank(major),"major",major);

        List<Student> stuList = studentService.list(stuWrapper);
        LinkedList<String> studentIdList = new LinkedList<>();
        for (Student stu: stuList) studentIdList.addFirst(stu.getStudentId());

        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.between(scoreRangeMin != null && scoreRangeMax != null,"score",scoreRangeMin,scoreRangeMax);
        wrapper.in(studentIdList.size() != 0,"student_id",studentIdList);
        List<Score> entityList = list(wrapper);
        // if(entityList.size() == 0) throw new CustomException(ResCodeEnum.BAD_REQUEST,"该教学班暂无该条件的成绩信息");

        //1. 所有学生列表，每一项处理后拷贝到 ExportVO的列表中
        ArrayList<FileScoreVO> exportVOList = new ArrayList<>();
        for (int i = 0; i < entityList.size(); i++) {
            Score entity = entityList.get(i);
            FileScoreVO exportVO = new FileScoreVO();
            exportVO.setOrder(i + 1);
            exportVO.setScore(entity.getScore());
            exportVO.setCourseCode(course.getCourseCode());
            exportVO.setCourseName(course.getCourseName());
            exportVO.setTeachClass(course.getTeachClass());

            for (Student stu: stuList) {
                if(entity.getStudentId().equals(stu.getStudentId())) {
                    exportVO.setGrade(stu.getGrade() + "级");
                    BeanUtils.copyProperties(stu, exportVO, "grade");
                    break;
                }
            }
            exportVOList.add(exportVO);
        }

        //2. 在UserExportVO类中给每个属性用 @Alias(value = "中文名")的注解添加中文名

        try {
            //3. 用ExcelUtil获取 excelWriter对象
            ExcelWriter excelWriter = ExcelUtil.getWriter(true);

            //4.将UserExportVO的列表写入excelWriter中，并添加中文名表头
            excelWriter.write(exportVOList, true);

            //5.设置浏览器响应的格式
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
            String fileName = URLEncoder.encode(course.getTeachClass() + "的学生成绩信息", "UTF-8");  //设置文件名称,并进行编码
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");  //将文件名设置到 response的 header中

            //6.用response来货如输出流
            ServletOutputStream outputStream = response.getOutputStream();

            //7. 将输出流刷新到 excelWriter对象中，并关闭输出流和excelWriter对象
            excelWriter.flush(outputStream, true);
            outputStream.close();
            excelWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(ResCodeEnum.SERVER_ERROR,"导出学生成绩信息表格失败");
        }
    }


    //返回空数据
    private ResData<PageResVO<ScoreVO>> returnEmptyRes() {
        //如果任何一个为空，说明没有该学生，或者没有该课程。进而选课表中就不会有数据，就直接返回空。
        PageResVO<ScoreVO> emptyPageResVO = new PageResVO<>();
        emptyPageResVO.setRows(new ArrayList<>());
        return ResData.success(emptyPageResVO);
    }
    private void validateScore(Score entity) {
        if(StrUtil.isBlank(entity.getCourseId())) throw new CustomException(ResCodeEnum.BAD_REQUEST,"课程id不能为空");
        if(StrUtil.isBlank(entity.getStudentId())) throw new CustomException(ResCodeEnum.BAD_REQUEST,"学生学号为空");
        if(StrUtil.isBlank(entity.getWorkId())) throw new CustomException(ResCodeEnum.BAD_REQUEST,"录入教师工号不能为空");
        if(entity.getScore() == null) throw new CustomException(ResCodeEnum.BAD_REQUEST,"分数不能为空");
    }
    // private ScoreStuChartVO buildScoreStuChartVO()
}
